package com.meilai.project.service.personal.handle.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.mapper.personal.handle.WFCheckHistoryMapper;
import com.meilai.project.mapper.workflow.*;
import com.meilai.project.service.personal.handle.PersonalHandleService;
import com.meilai.project.util.ThreadLocalUserUtil;
import com.meilai.project.vo.personal.handle.CheckHistoryVO;
import com.meilai.project.vo.personal.handle.HandleCountsVO;
import com.meilai.project.vo.personal.handle.PersonalHandleVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张驰
 * @date 2022/1/11 16:46
 */
@Service
public class PersonalHandleServiceImpl implements PersonalHandleService {
    @Autowired
    private VWFDataProcessingMapper vwfDataProcessingMapper;
    @Autowired
    private VWFDataFinishedMapper vwfDataFinishedMapper;
    @Autowired
    private VWFDataBackMapper vwfDataBackMapper;
    @Autowired
    private VWFDataDraftMapper vwfDataDraftMapper;
    @Autowired
    private VWFDataDeleteMapper vwfDataDeleteMapper;
    @Autowired
    private VWFDataProcessingLogMapper vwfDataProcessingLogMapper;
    @Autowired
    private WFCheckHistoryMapper wFCheckHistoryMapper;

    @Override
    public Page<PersonalHandleVO> getToCheckList(Integer current_page, Integer page_size, boolean is_web) {
        Page<PersonalHandleVO> page = new Page<PersonalHandleVO>(current_page, page_size);
        List<PersonalHandleVO> list = vwfDataProcessingMapper.selectToCheckPageList(page, ThreadLocalUserUtil.get().getId(), is_web);
        return page.setRecords(list);
    }

    @Override
    public Page<PersonalHandleVO> getProcessingList(Integer current_page, Integer page_size, boolean is_web) {
        Page<PersonalHandleVO> page = new Page<PersonalHandleVO>(current_page, page_size);
        List<PersonalHandleVO> list = vwfDataProcessingMapper.selectPageList(page, ThreadLocalUserUtil.get().getId(), is_web);
        return page.setRecords(list);
    }

    @Override
    public Page<PersonalHandleVO> getFinishedList(Integer current_page, Integer page_size, boolean is_web) {
        Page<PersonalHandleVO> page = new Page<PersonalHandleVO>(current_page, page_size);
        List<PersonalHandleVO> list = vwfDataFinishedMapper.selectPageList(page, ThreadLocalUserUtil.get().getId(), is_web);
        return page.setRecords(list);
    }

    @Override
    public Page<PersonalHandleVO> getBackList(Integer current_page, Integer page_size, boolean is_web) {
        Page<PersonalHandleVO> page = new Page<PersonalHandleVO>(current_page, page_size);
        List<PersonalHandleVO> list = vwfDataBackMapper.selectPageList(page, ThreadLocalUserUtil.get().getId(), is_web);
        return page.setRecords(list);
    }

    @Override
    public Page<PersonalHandleVO> getDraftList(Integer current_page, Integer page_size, boolean is_web) {
        Page<PersonalHandleVO> page = new Page<PersonalHandleVO>(current_page, page_size);
        List<PersonalHandleVO> list = vwfDataDraftMapper.selectPageList(page, ThreadLocalUserUtil.get().getId(), is_web);
        return page.setRecords(list);
    }

    @Override
    public Page<PersonalHandleVO> getDeleteList(Integer current_page, Integer page_size, boolean is_web) {
        Page<PersonalHandleVO> page = new Page<PersonalHandleVO>(current_page, page_size);
        List<PersonalHandleVO> list = vwfDataDeleteMapper.selectPageList(page, ThreadLocalUserUtil.get().getId(), is_web);
        return page.setRecords(list);
    }

    @Override
    public Page<PersonalHandleVO> getCheckedList(Integer current_page, Integer page_size, boolean is_web) {
        Page<PersonalHandleVO> page = new Page<PersonalHandleVO>(current_page, page_size);
        List<PersonalHandleVO> list = vwfDataProcessingLogMapper.selectCheckedPageList(page, ThreadLocalUserUtil.get().getId(), is_web);
        return page.setRecords(list);
    }

    @Override
    public List<CheckHistoryVO> getCheckHistory(Long wf_data_id, Long data_id) {
        List<CheckHistoryVO> list = wFCheckHistoryMapper.selectCheckHistoryList(wf_data_id, data_id);
        return list;
    }

    @Override
    public List<CheckHistoryVO> getCheckHistory2(Integer flow_type, Long data_id) {
        List<CheckHistoryVO> results = wFCheckHistoryMapper.selectCheckHistoryList2(flow_type, data_id);
        if (CollectionUtils.isNotEmpty(results)) {
            if (results.get(0).getWd_status() == 2) { // 流程数据已通过
                CheckHistoryVO last = results.get(0);
                results.add(0, new CheckHistoryVO(-1L, last.getCreate_at(), -1, 2));
            }
        } else {
            results = new ArrayList<>();
        }
        return results;
    }

    @Override
    public HandleCountsVO getCounts() {
        HandleCountsVO result = new HandleCountsVO();
        result.setTo_check_count(vwfDataProcessingMapper.getToCheckCount(ThreadLocalUserUtil.get().getId()));
        return result;
    }
}
